﻿using GatewayClient.MQ;
using GatewayClient.Orangepi;
using IotDeviceToolHepler.Utils;
using IotDeviceToolHepler.WiringOPSharp;
using Models.Common;
using Models.Constant;
using Models.Device;
using Models.TaskPlain;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UtilsBase;
using static Models.Common.EnumDeviceState;
using static Models.Common.EnumDeviceType;

namespace GatewayClient.Device
{
    public class ServiceDeviceGpio
    {

        public void gateWayExecuteCommand(object obj)
        {
            #region
            MQReqModel<object> mQReqModel = (MQReqModel<object>)obj;
            CommandLogModel commandLogModel = mQReqModel.toGatewayData;

            Console.WriteLine("DeviceReciveGpioCommand 时间：" + DateTime.Now);
            try
            {

                // Console.WriteLine("初始定时检测");
                //Console.WriteLine("待执行指令:" + JsonConvert.SerializeObject(mQReqModel.data));
                ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                ModelCommandAgreementParameter modelCommandAgreementGpio = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);



                //gpio引强断电器
                //  Console.WriteLine("gpio 执行");
                if (commandLogModel.key == EnumGpioCommand.check_state || commandLogModel.key == EnumGpioCommand.check_out_state)
                {

                    //查状态
                    string check_state = new UtilsOrangePiGpio().getGpioOutputMode(modelCommandAgreementGpio.wpi);
                    ModelReceverData modelDeviceData = new ModelReceverData();
                    modelDeviceData.content = check_state;
                    mQReqModel.toServerData = modelDeviceData;

                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    return;
                }
                else if (commandLogModel.key == EnumGpioCommand.check_in_state)
                {

                    //查状态
                    string check_state = UtilsOrangePiGpio.getGpioInputMode(modelCommandAgreementGpio.wpi);
                    ModelReceverData modelDeviceData = new ModelReceverData();
                    modelDeviceData.content = check_state;
                    mQReqModel.toServerData = modelDeviceData;

                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    return;
                }
                else if (commandLogModel.key == EnumGpioCommand.open || commandLogModel.key == EnumGpioCommand.close)
                {
                    //执行开关
                    string result = new UtilsOrangePiGpio().setGpioOutputMode(commandLogModel.content, modelCommandAgreementGpio.wpi);
                    ModelReceverData modelDeviceData = new ModelReceverData();
                    modelDeviceData.content = result;
                    mQReqModel.toServerData = modelDeviceData;
                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    return;
                }
                else if (commandLogModel.key == EnumGpioCommand.startCallBack)
                {
                    //开始监听
                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    Task.Run(() =>
                    {
                        new OrangePIGpioValueChange().eventCallbackByGpio(mQReqModel);
                    });
                }
                else if (commandLogModel.key == EnumGpioCommand.stopCallBack)
                {
                    //停止监听
                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    Task.Run(() =>
                    {
                        new OrangePIGpioValueChange().returnGpioValueChangeStop(mQReqModel);
                    });
                }


            }
            catch (Exception ex)
            {
                mQReqModel.respCode = ResponseCode.System_ERROR;
                mQReqModel.respMsg = ex.Message;
                new ServiceMQ_Helper().sendMsg(mQReqModel);
                Console.WriteLine(DateTime.Now.ToString() + ex);
            }
            #endregion
        }

    }
}
